# 函数返回值
def calc(a, b):
    return a + b


def isNotReturnCalc(a, b):
    print(a + b)


print(isNotReturnCalc(2, 3))

print(calc(2, 3))


# 返回值多个
def fun():
    return 1, 2, 3


v = fun()

print(v)

a, b, c = fun()
print(a, b, c)

val = 100


# 变量作用域
def fun2(a, b):
    return val + a + b


print(fun2(2, 3))
print(val)


def fun3(a, b):
    val = 200
    return val + a + b


print(fun3(2, 3))
print(val)


def fun4(a, b):
    global val
    val = 1000
    return val + a + b


print(fun4(2, 3))
print(val)

# 匿名函数的使用
sum_to = lambda a, b: (a + b)
print(sum_to(2, 3))
print(sum_to('马', '迪迪'))
print(type(sum_to(4, 5)))

# 使用匿名函数
lst = [11, 21, 31, 41, 51]
for i in range(len(lst)):
    print(lst[i])

print('*' * 20 + '*' * 20)
# 使用匿名函数便利
for i in range(len(lst)):
    result = lambda x: x[i]
    print(result(lst))

print('*' * 20 + 'lambda 式排序' + '*' * 20)
# 对列表进行排序，lamdba表达式
student_scores = [
    {'name': '麦迪80', 'score': 80},
    {'name': '麦迪90', 'score': 90},
    {'name': '麦迪85', 'score': 85},
    {'name': '麦迪66', 'score': 66},
]

student_scores.sort(key=lambda x: x['score'], reverse=True)
print(student_scores)

# 函数递归
print('*' * 20, '函数递归', '*' * 20)


# 使用数学中阶乘 5！= 5*4*3*2*1
def funNum(n):
    if n == 1:
        return 1
    else:
        return n * funNum(n - 1)


print(funNum(5))

# 经典案例 斐波那契数列 公式：fn = f（n-1） + f （n-2） 又称黄金分割线
print('*' * 20, '斐波那契数列', '*' * 20)


def fac(n):
    if n == 1 or n == 2:
        return 1
    else:
        return fac(n - 1) + fac(n - 2)


print(fac(10))
print('*' * 20, '循环打印各个数字', '*' * 20)
for i in range(10):
    print(fac(i + 1))

y = [10, 20, 30, 40, 50]
x = ['a', 'b', 'c', 'd', 'e']
enum = enumerate(y, start=2)
print(type(enum))
print(tuple(enum))
print(type(dict(enum)))

# next只能使用迭代器对象
zipobj = zip(x, y)
reversedObj = reversed(y)
print(next(zipobj))
print(next(reversedObj))


# filter函数
def funA(num):
    return num % 2 == 1


objFilter = filter(funA, range(10))
print(list(objFilter))


# map 函数的使用
def funB(num):
    return num.upper()

objMap = map(funB, ['a', 'b', 'c', 'd', 'e'])

print(list(objMap))

print(id(objMap))
print(id(list(objMap)))